﻿#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
char mine[ROWS][COLS];
char show[ROWS][COLS];
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void digui(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y) {
	if (x < 1 || x > ROW || y < 1 || y > COL || show[x][y] != '*') {
		return;
	}
	int cnt = leicnt(mine, x, y);
	if (cnt == 0) {
		show[x][y] = ' ';
		for (int i = -1; i <= 1; i++) {
			for (int j = -1; j <= 1; j++) {
				digui(show, mine, x + i, y + j);
			}
		}
	}
	else {
		show[x][y] = cnt + '0';
	}
}
void menu() {
	printf("---------扫雷---------\n");
	printf("---------1.start------\n");
	printf("---------0.exit--------\n");

	return;
}
int count = 10;
void init1(char a[ROWS][COLS], int r, int c) {
	///每个位置都设为'0'
	for (int i = 0;i < r;i++) {
		for (int j = 0;j < c;j++) {
			a[i][j] = '0';
		}
	}
	return;
}
void init2(char a[ROWS][COLS], int r, int c) {
	for (int i = 0;i < r;i++) {
		for (int j = 0;j < c;j++) {
			a[i][j] = '*';
		}
	}
	return;
}
//埋雷
void mailei(char a[ROWS][COLS], int r, int c) {
	count = 10;
	while (count) {
		int x = rand() % r + 1;
		int y = rand() % c + 1;
		if (a[x][y] == '1') {
			continue;
		}
		else {
			a[x][y] = '1';
			count--;
		}
	}
	return;
}
void printlei(char a[ROWS][COLS], int r, int l) {
	for (int i = 0;i <= l;i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1;i <= r;i++) {
		printf("%d ", i);
		for (int j = 1;j <= l;j++) {

			printf("%c ", a[i][j]);
		}
		printf("\n");
	}
}
int leicnt(char mine[ROWS][COLS], int m, int q) {
	return mine[m - 1][q - 1] + mine[m - 1][q] + mine[m - 1][q + 1] + mine[m][q + 1] + mine[m + 1][q + 1] + mine[m + 1][q] + mine[m + 1][q - 1] + mine[m][q - 1] - 8 * '0';

}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - 10)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				printlei(mine, ROW, COL);
				break;
			}
			else
			{
				//该位置不是雷，就统计这个坐标周围有⼏个雷 
				int cnt = leicnt(mine, x, y);
				if (cnt == 0) {
					digui(show, mine, x, y);
				}
				else { show[x][y] = cnt + '0'; }
				printlei(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法，重新输入\n");
		}
	}
	if (win == row * col - 10)
	{
		printf("恭喜你，排雷成功\n");
		printlei(mine, ROW, COL);
	}
}
void game() {
	printlei(mine, ROW, COL);
	printf("\n");
	printlei(show, ROW, COL);
	FindMine(mine, show, ROW, COL);

}
int main() {
	srand((unsigned int)time(NULL));
	int input = 0;
	do {
		init1(mine, ROWS, COLS);
		init2(show, ROWS, COLS);
		menu();
		scanf("%d", &input);
		if (input == 1) {
			mailei(mine, ROW, COL);
			game();
		}
		else if (input == 0) {
			break;
		}
		else {
			printf("只能输入1或0，请重新输入:\n");
		}
	} while (input);
	return 0;
}